package com.frxuexi.mine.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath
import com.tencent.mmkv.MMKV

@Composable
fun MinePage(navHostController: NavHostController) {
    var uname by remember {
        mutableStateOf(MMKV.defaultMMKV().decodeString("uname","未登录").toString())
    }
    var isShow by remember {
        mutableStateOf(false)
    }
    var modi= Modifier
        .fillMaxWidth()
        .background(Color.White, RoundedCornerShape(20.dp))
        .border(1.dp, Color.LightGray, RoundedCornerShape(20.dp))
        .padding(20.dp)
    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp), verticalArrangement = Arrangement.spacedBy(20.dp)) {
        Text(text = "我的", fontSize = 28.sp, fontWeight = FontWeight.Bold)

        Row(
            modi.clickable {
                if(uname=="未登录"){
                    navHostController.navigate(RoutePath.LOGIN)
//                    navHostController.navigate(RoutePath.ROOMLOGIN)
                }else{
                    isShow=true
                }
            }) {
            AsyncImage(model = "https://img0.baidu.com/it/u=797474174,3214837072&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1732294800&t=f472f47d1015da000b2194ee3b7a3c4a", contentDescription = "",
                modifier = Modifier
                    .size(80.dp)
                    .clip(CircleShape), contentScale = ContentScale.Crop)
                Text(text = uname)

        }

        Row(modi) {
            val size = Modifier.size(30.dp)
            Column(Modifier.clickable {
                navHostController.navigate(RoutePath.LEAVELIST)
            }) {
                Image(painter = painterResource(id = R.drawable.icon359), contentDescription = "",
                    modifier = size)
                Spacer(modifier = Modifier.height(10.dp))
                Text(text = "请假")
            }
            Spacer(modifier = Modifier.width(10.dp))
            Column(Modifier.clickable {
                navHostController.navigate(RoutePath.APPROVELEAVE)
            }) {
                Image(painter = painterResource(id = R.drawable.icon105), contentDescription = "",
                    modifier = size)
                Spacer(modifier = Modifier.height(10.dp))
                Text(text = "审批")
            }
        }
    }

    if(isShow){
        AlertDialog(onDismissRequest = { isShow=false }, confirmButton = {
            Text(text = "确认",Modifier.clickable { 
                MMKV.defaultMMKV().remove("token")
                MMKV.defaultMMKV().remove("uname")
                uname="未登录"
                isShow=false
            })
        }, title = { Text(text = "退出提醒")}, text = { Text(text = "是否退出登录？")})
    }


}